<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title>壁纸一览</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" />
</head>

<body>
    <div id="app">
        <el-container>
            <el-header>
                <h1>壁纸一览</h1>
            </el-header>
            <el-main>
                <el-form>
                    <el-form-item label="">
                        <el-input v-model="filters.title" placeholder="输入您想查询的标题">
                            <el-button :loading="listLoading" slot="append" icon="el-icon-search"
                                @click="handleCurrentChange(1)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-form>
                <div class="block">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page.sync="page" :page-sizes="[5, 10, 20]" :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper" :total="total">
                    </el-pagination>
                </div>
                <el-table :data="items" style="width: 100%" v-loading="listLoading" @sort-change="sortChange">
                    <el-table-column prop="url" label="图片" width="180">
                        <template slot-scope="scope">
                            <el-image :src="'https://cn.bing.com' + scope.row.url"></el-image>
                        </template>
                    </el-table-column>
                    <el-table-column prop="title" label="标题" sortable="custom"> </el-table-column>
                    <el-table-column prop="copyright" label="版权" sortable="custom"> </el-table-column>
                </el-table>
            </el-main>
        </el-container>
    </div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://unpkg.com/qs/dist/qs.js"></script>
<script>
    new Vue({
        el: '#app',
        data: function () {
            return {
                filters: {
                    title: ''
                },
                items: [],
                total: 0,
                page: 1,
                pageSize: 5,
                order: '',
                sortby: '',
                sels: [],
                listLoading: false
            }
        },
        mounted() {
            this.getList()
        },
        methods: {
            handleCurrentChange(val) {
                this.page = val
                this.getList()
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.getList()
            },
            selsChange: function (sels) {
                this.sels = sels
            },
            sortChange(column) {
                const vue = this
                if (column.order === 'ascending') {
                    vue.order = 'asc'
                } else if (column.order === 'descending') {
                    vue.order = 'desc'
                }
                vue.sortby = column.prop
                vue.getList()
            },
            // 获取用户列表
            getList() {
                const vue = this
                let params = {
                    page: vue.page,
                    size: vue.pageSize,
                    order: vue.order,
                    sortby: vue.sortby,
                    title: vue.filters.title
                }
                vue.listLoading = true
                fetch(`/wallpapers?${Qs.stringify(params)}`)
                    .then(data => data.json())
                    .then(data => {
                        vue.listLoading = false
                        if (data.status === 200) {
                            vue.total = data.records
                            vue.items = data.rows
                        }
                    })
            }
        }
    })
</script>

</html>